import 'package:flutter/material.dart';
import 'package:ball_lion/module/record/items/road_match_item.dart';
import 'package:ball_lion/entity/record/points_entity.dart';
import 'package:ball_lion/utils/utils.dart';

class PointsChart extends StatelessWidget {
  const PointsChart({super.key, required this.road, required this.recordType});
  const PointsChart.soccer({super.key, required this.road}) : recordType = 0;
  const PointsChart.basketball({super.key, required this.road})
      : recordType = 1;

  final Road? road;
  final int recordType;

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          if (road?.roadVs16.hasValue == true)
            Padding(
                padding: const EdgeInsets.only(bottom: 8), child: round(16)),
          if (road?.roadVs8.hasValue == true)
            Padding(padding: const EdgeInsets.only(bottom: 8), child: round(8)),
          if (road?.roadVs4.hasValue == true)
            Padding(padding: const EdgeInsets.only(bottom: 8), child: round(4)),
          RoadMatchItem.champion(
              match: road?.roadVs?[0],
              round: 1,
              inBottom: false,
              recordType: recordType),
          if (road?.roadVs4.hasValue == true)
            Padding(
                padding: const EdgeInsets.only(top: 8),
                child: round(4, inBottom: true)),
          if (road?.roadVs8.hasValue == true)
            Padding(
                padding: const EdgeInsets.only(top: 8),
                child: round(8, inBottom: true)),
          if (road?.roadVs16.hasValue == true)
            Padding(
                padding: const EdgeInsets.only(top: 8),
                child: round(16, inBottom: true)),
        ],
      ),
    );
  }

  Widget round(int round, {bool inBottom = false}) {
    List<ScheduleGroup>? entity;
    switch (round) {
      case 16:
        entity = road?.roadVs16;
        break;
      case 8:
        entity = road?.roadVs8;
        break;
      case 4:
        entity = road?.roadVs4;
        break;
    }
    List<Widget> list =
        List.generate(round ~/ 4, (index) => Expanded(child: Container()));
    if (entity?.length == round ~/ 2) {
      list = entity!
          .sublist(
              inBottom ? round ~/ 4 : 0, inBottom ? round ~/ 2 : round ~/ 4)
          .map<Widget>((e) => Expanded(
                child: recordType == 0
                    ? RoadMatchItem.soccer(
                        inBottom: inBottom,
                        match: e,
                        round: round,
                      )
                    : RoadMatchItem.basketball(
                        inBottom: inBottom,
                        match: e,
                        round: round,
                      ),
              ))
          .toList();
    } else {
      entity?.forEach((element) {
        if (element.sort.hasValue &&
            (inBottom
                ? (element.sort! > round ~/ 4 && element.sort! < round ~/ 2)
                : element.sort! <= round ~/ 4)) {
          list[element.sort! - (inBottom ? 5 : 1)] = Expanded(
              child: recordType == 0
                  ? RoadMatchItem.soccer(
                      inBottom: inBottom,
                      match: element,
                      round: round,
                    )
                  : RoadMatchItem.basketball(
                      inBottom: inBottom,
                      match: element,
                      round: round,
                    ));
        }
      });
    }
    return Row(crossAxisAlignment: CrossAxisAlignment.start, children: list);
  }
}
